#define BOOST_TEST_MODULE testElementRole

#include <boost/test/unit_test.hpp>

#include "common/elements/element_maker.h"
#include "common/elements/role.h"

BOOST_AUTO_TEST_CASE(testRoleJson)
{
    auto first = std::make_shared<Elements::Role>();
    first->setId(1);
    first->setCaption("a");
    first->setDescription("b");

    Elements::BasePermissions firstBasePermissions;
    firstBasePermissions.setFlag(Elements::BasePermission::FullAdmin);
    firstBasePermissions.setFlag(Elements::BasePermission::PlanCorrector);
    first->setBasePermissions(firstBasePermissions);

    const auto json = first->toJsonString();
    const Elements::ElementPtr second(Elements::fromJsonString(json));
    BOOST_CHECK(first->isSame(second.get()));

    const auto third = std::static_pointer_cast<Elements::Role>(second);
    BOOST_CHECK(1 == third->id());
    BOOST_CHECK("a" == third->caption());
    BOOST_CHECK("b" == third->description());

    const auto thirdBasePermissions = third->basePermissions();
    BOOST_CHECK(false == thirdBasePermissions.testFlag(Elements::BasePermission::None));
    BOOST_CHECK(true == thirdBasePermissions.testFlag(Elements::BasePermission::FullAdmin));
    BOOST_CHECK(false == thirdBasePermissions.testFlag(Elements::BasePermission::DaysCorrector));
    BOOST_CHECK(false == thirdBasePermissions.testFlag(Elements::BasePermission::UserCorrector));
    BOOST_CHECK(false == thirdBasePermissions.testFlag(Elements::BasePermission::WorkflowCorrector));
    BOOST_CHECK(false == thirdBasePermissions.testFlag(Elements::BasePermission::BoardCorrector));
    BOOST_CHECK(false == thirdBasePermissions.testFlag(Elements::BasePermission::ProjectCorrector));
    BOOST_CHECK(true == thirdBasePermissions.testFlag(Elements::BasePermission::PlanCorrector));
}

BOOST_AUTO_TEST_CASE(testRoleBasePermissions)
{
    {
        Elements::BasePermissions permissions;
        BOOST_CHECK(true == permissions.testFlag(Elements::BasePermission::None));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::FullAdmin));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::DaysCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::UserCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::WorkflowCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::BoardCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::ProjectCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::PlanCorrector));
    }

    {
        Elements::BasePermissions permissions;
        permissions.setFlag(Elements::BasePermission::FullAdmin);
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::None));
        BOOST_CHECK(true == permissions.testFlag(Elements::BasePermission::FullAdmin));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::DaysCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::UserCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::WorkflowCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::BoardCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::ProjectCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::PlanCorrector));
    }

    {
        Elements::BasePermissions permissions;
        permissions.setFlag(Elements::BasePermission::FullAdmin);
        permissions.setFlag(Elements::BasePermission::DaysCorrector);
        permissions.setFlag(Elements::BasePermission::UserCorrector);
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::None));
        BOOST_CHECK(true == permissions.testFlag(Elements::BasePermission::FullAdmin));
        BOOST_CHECK(true == permissions.testFlag(Elements::BasePermission::DaysCorrector));
        BOOST_CHECK(true == permissions.testFlag(Elements::BasePermission::UserCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::WorkflowCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::BoardCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::ProjectCorrector));
        BOOST_CHECK(false == permissions.testFlag(Elements::BasePermission::PlanCorrector));
    }
}
